﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

class Piston : SceneObject      //这能是横着动  或者竖着动  //并且初始化 好了之后不得修改
{
    public enum MoveDirection
    {
        Horizontal,//水平的
        Vertical,   //竖直的
    }
    public int SizeX = 1;   //只占整数格子
    public int SizeY = 1;
    public float offsetSize = 0.0f;
    public Vector2 InitPosition;    //初始化后的位置
    public Vector2 LastFramePosition;   //上一次计算的时候的位置
    public MoveDirection CurrentMoveDirection;

    public override void Init()
    {
        InitPosition = transform.position;
        LastFramePosition = transform.position;
        
    }

    public override void UpdateWrap(float deltaTime)
    {

    }

    public override void UpdatePerFrame()
    {
        base.UpdatePerFrame();
        //根据自己的位置  设置airblock
        Vector3 position = transform.position;
        Rect oldrect = new Rect(LastFramePosition.x - SizeX / 2.0f, LastFramePosition.y - SizeY / 2.0f, SizeX, SizeY);
        Rect rect = new Rect(position.x - SizeX / 2.0f, position.y - SizeY / 2.0f, SizeX, SizeY);
        float minX = Math.Min(oldrect.x, rect.x);
        float miny = Math.Min(oldrect.y, rect.y);
        float maxx = Math.Max(oldrect.xMax, rect.xMax);
        float maxy = Math.Max(oldrect.yMax, rect.yMax);
        Rect toTestRect = new Rect(minX, miny, maxx - minX, maxy - miny);

        for (int i = Mathf.FloorToInt(toTestRect.x);i < Mathf.CeilToInt(toTestRect.x + toTestRect.width);i++)
        {
            for(int j = Mathf.FloorToInt(toTestRect.y); j < Mathf.CeilToInt(toTestRect.y + toTestRect.height);j++)
            {
                MatterGrid mg = MainGame.Instance.Scene.GetGrid(i, j);

                //0为上 1为右 2 为下  3为左
                if (mg != null)
                {
                    mg.SetAirBlock(0, 0.0f);
                    mg.SetAirBlock(1, 0.0f);
                    mg.SetAirBlock(2, 0.0f);
                    mg.SetAirBlock(3, 0.0f);    //先全部清空

                    if (i < rect.x - 1)
                    {
                        mg.SetAirBlock(1, 0.0f);
                        mg.SetAirBlock(3, 0.0f);
                    }
                    else if (i < rect.x) //在活塞的左边边界
                    {
                        mg.SetAirBlock(3, 1 - (rect.x - i));
                    }
                    else if( i < rect.xMax - 1)
                    {
                        mg.SetAirBlock(1, 1.0f);
                    }
                    else if( i < rect.xMax)
                    {
                        mg.SetAirBlock(1,1 + (i-rect.xMax));
                    }
                    else
                    {
                        mg.SetAirBlock(1, 0.0f);
                        mg.SetAirBlock(3, 0.0f);
                    }

                    if (j < rect.yMin - 1)
                    {
                        mg.SetAirBlock(0, 0.0f);
                        mg.SetAirBlock(2, 0.0f);
                    }
                    else if (j < rect.yMin) //在活塞的上面
                    {
                        mg.SetAirBlock(2, 1 - (rect.yMin - i));
                    }
                    else if (j < rect.yMax - 1)
                    {
                        mg.SetAirBlock(0, 1.0f);
                    }
                    else if (j < rect.yMax)
                    {
                        mg.SetAirBlock(0, 1 + (i - rect.yMax));
                    }
                    else
                    {
                        mg.SetAirBlock(0, 0.0f);
                        mg.SetAirBlock(2, 0.0f);
                    }
                }
            }
        }

    }

    public override int GetHeight()
    {
        return SizeY;
    }

    public override int GetWidth()
    {
        return SizeX;
    }

    public override float GetMass()
    {
        return 1.0f;
    }
}
